package com.stone.framework.user.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.stone.framework.user.entity.RoleResource;

@Mapper
public interface PermissionMapper extends BaseMapper<RoleResource> {

    @Select("SELECT SRE.ID, SRE.PARENT_ID, SRE.NAME, SRE.LABEL_, SRE.ICON, SRE.TYPE, SRE.ORDER_ FROM ST_ROLE_USER SRU JOIN ST_USER SU ON SU.ID = SRU.USER_ID JOIN ST_ROLE SR ON SR.ID = SRU.ROLE_ID JOIN ST_ROLE_RESOURCE SRR ON SR.ID = SRR.ROLE_ID JOIN ST_RESOURCE SRE ON SRR.RESOURCE_ID = SRE.ID WHERE SR.STATE = 'ENABLED' AND SRE.STATE = 'ENABLED' AND SU.NAME = #{username}")
    List<Map<String, Object>> selectByUsername(String username);

    @Select("SELECT SR.ID, SR.NAME, SR.DESC_, SR.ORDER_, IF(SRR.ID IS NULL, 'N', 'Y') AS GRANTED, SR.PARENT_ID FROM ST_RESOURCE SR LEFT JOIN ST_ROLE_RESOURCE SRR ON SR.ID = SRR.RESOURCE_ID AND SRR.ROLE_ID = #{roleId} WHERE SR.STATE = 'ENABLED'")
    List<Map<String, Object>> selectByRoleId(Long roleId);

}
